from datetime import date, datetime, timedelta, timezone

now = datetime.now()
print('现在时间是%s' % now)
print('datetime的类型是%s' % type(now))

custizeTime = datetime(2021, 1, 9, 8, 40)
print('自定义的时间是%s' % custizeTime)

custizeTimeStamp = custizeTime.timestamp()
print('自定义时间的timestamp是%s' % custizeTimeStamp)

transferTime = datetime.fromtimestamp(custizeTimeStamp)
print('timestamp转换成datetime对象：%s' % transferTime)

standTransferTime = datetime.utcfromtimestamp(custizeTimeStamp)
print('timestamp转换成utc标准时区对象:%s' % standTransferTime)


timeStr = '2021-5-13 15:14:00'
str2Datetime = datetime.strptime(timeStr, '%Y-%m-%d %H:%M:%S')
print('字符串转换成datetime对象为：%s, 数据类型为：%s' % (str2Datetime, type(str2Datetime)))

plusTime = now + timedelta(hours=4)
print('当前时间往后推4个小时的时间：%s' % plusTime)
subtractTime = now - timedelta(days=1)
print('当前时间往前推一天的时间：%s' % subtractTime)

utcNowTime  = datetime.utcnow().replace(tzinfo=timezone.utc)
hour8UtcTime = utcNowTime.astimezone(timezone(timedelta(hours=8)))
print('转换成东八区time：%s' % hour8UtcTime)

